package com.sc.controller.patrol;

import com.sc.domain.patrol.PatrolTask;
import com.sc.dto.PatrolTaskDTO;
import com.sc.result.PageResult;
import com.sc.result.Result;
import com.sc.service.patrol.PatrolTaskService;
import com.sc.vo.PatrolTaskVO;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 巡更任务管理
 * 功能：增删改查、作列表展示
 * @author:Zuiliu
 **/
@CrossOrigin
@RestController
@RequestMapping("/patrolTask")
@RequiredArgsConstructor
public class PatrolTaskController {

    private final PatrolTaskService ivrs;

    // 添加巡更任务
    @PostMapping("/addPatrolTask")
    public Result addPatrolTask(@RequestBody PatrolTask patrolTask) {
        try {
            return Result.success("添加成功", ivrs.save(patrolTask));
        } catch (DuplicateKeyException e) {
            return Result.error("巡更任务名称重复，换一个试试");
        }
    }

    // 删除巡更任务
    @DeleteMapping("/deleteById")
    public Result deleteById(Integer id) {
        return Result.success("删除成功", ivrs.removeById(id));
    }

    @DeleteMapping("/deleteByIds")
    public Result deleteByIds(@RequestBody List<Integer> ids) {
        return Result.success("删除成功", ivrs.removeByIds(ids));
    }

    // 修改巡更任务
    @PutMapping("/updateById")
    public Result updateById(@RequestBody PatrolTask patrolTask) {
        try {
            return Result.success("修改成功", ivrs.updateById(patrolTask));
        } catch (DuplicateKeyException e) {
            return Result.error("巡更任务名称重复，换一个试试");
        }
    }

    // 查询巡更任务
    @GetMapping("/selectById")
    public Result<PatrolTask> selectById(Integer id) {
        return Result.success(ivrs.getById(id));
    }

    // 列表展示
    @PostMapping("/listPatrolTask")
    public Result<PageResult<PatrolTaskVO>> listPatrolTask(@RequestBody PatrolTaskDTO patrolTaskDTO) {
        return Result.success(ivrs.listPatrolTask(patrolTaskDTO));
    }
}
